#include "vector2f.h"

using namespace bt;

Vector2f::Vector2f()
{
	x = 0;
	y = 0;
}

Vector2f::Vector2f(const Vector2f& rhs)
{
	x = rhs.x;
	y = rhs.y;
}

Vector2f::Vector2f(float x, float y)
{
	this->x = x;
	this->y = y;
}

Vector2f::~Vector2f()
{
}

//----------------------------------------------------------------------------
// Assignment Operators
//----------------------------------------------------------------------------
Vector2f& Vector2f::operator=(const Vector2f& rhs)
{
	x = rhs.x;
	y = rhs.y;
	return *this;	// Self reference
}

Vector2f& Vector2f::operator+=(const Vector2f& rhs)
{
	x += rhs.x;
	y += rhs.y;
	return *this;
}

Vector2f& Vector2f::operator-=(const Vector2f& rhs)
{
	x -= rhs.x;
	y -= rhs.y;
	return *this;
}

Vector2f& Vector2f::operator*=(const float& rhs)
{
	x *= rhs;
	y *= rhs;
	return *this;
}

//----------------------------------------------------------------------------
// Comparison Operators
//----------------------------------------------------------------------------
bool Vector2f::operator==(const Vector2f& rhs)
{
	bool b = true;
	
	if((x != rhs.x) || (y != rhs.y))
		b = false;
	
	return b;
}
	
bool Vector2f::operator!=(const Vector2f& rhs)
{
	return !(*this == rhs);
}

//----------------------------------------------------------------------------
// Binary Operators
//----------------------------------------------------------------------------
const Vector2f Vector2f::operator+(const Vector2f& rhs)
{
	Vector2f v(*this);		// Create copy of current values
	v += rhs;				// Add right hand vector
	return v;				// return reference to the new vector
}

const Vector2f Vector2f::operator-(const Vector2f& rhs)
{
	Vector2f v(*this);		// Create copy of current values
	v -= rhs;				// Subtract right hand vector
	return v;				// return reference to the new vector
}

const Vector2f Vector2f::operator*(const float& rhs)
{
	Vector2f v(*this);		// Create copy of current values
	v *= rhs;				// Add right hand vector
	return v;				// return reference to the new vector
}

const Vector2f operator*(const float& lhs, Vector2f& rhs)
{
	Vector2f v(rhs * lhs);
	return v;
}

const float Vector2f::dot(const Vector2f& rhs)
{
	return (x*rhs.x + y*rhs.y);
}

const Vector2f Vector2f::proj(Vector2f& rhs)
{
	float l = length();

	if(l != 0)
		return *this * (dot(rhs) / (l*l));
	else
		return Vector2f(0.0f, 0.0f);
}

std::ostream& operator<<(std::ostream& os, const Vector2f& rhs)
{
	return os << "<" << rhs.x << "," << rhs.y << ">";
}

//----------------------------------------------------------------------------
// Member Functions
//----------------------------------------------------------------------------
const float Vector2f::length()
{
	return sqrt(x*x + y*y);
}
